import * as koa from 'koa2';
import * as cors from 'koa2-cors'
import * as Router from 'koa-router';
import crypto from '../utils/crypto';
import  { httpRouterMap } from '../configs_win';







const app = new koa();
const router = new Router();
let config: account_server = null;
/**大厅服务器的地址 */
let hallAddr = '';


// 设置跨域
app.use(cors());
// 设置路由
app.use(router.routes()).use(router.allowedMethods());


export default class as{
    /**
     * 开启账号服务器  44
     * @param cfg 
     */
    static start(cfg: account_server){
        config = cfg;
        hallAddr = config.HALL_IP + ':' + config.HALL_CLIENT_PORT;
        app.listen(config.CLIENT_PORT, () => {
            console.log(`账号服务器运行在： ${config.CLIENT_PORT}端口`);
        });
    }
}













router.get(httpRouterMap.GET_SERVERINFO, get_serverinfo);
router.get(httpRouterMap.GUEST, guest);

















/**
 * 返回服务器版本号、下载地址、大厅地址
 * @param ctx 
 */
async function get_serverinfo(ctx: Router.IRouterContext){
    const ret = {
        version: config.VERSION,
        hall: hallAddr,
        // hall: '192.168.188.35:9001',
        appweb: config.APP_WEB,
    };
    ctx.body = ret;
}
/**
 * 游客登陆
 * @param ctx 
 */
async function guest(ctx: Router.IRouterContext){
    const account = 'guest_' + ctx.query.account;
    const sign = crypto.md5(account + ctx.ip + config.ACCOUNT_PRI_KEY);
    const ret: ret = {
        errcode: 0,
        errmsg: 'ok',
        account,
        halladdr: hallAddr,
        sign,
    };
    ctx.body = ret;
}


